package cn.aishua.ui.main;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.LinearLayout.LayoutParams;

import cn.aishua.data.ResourceDataParser;
import cn.aishua.data.ResourceDataParser.Item;
import cn.aishua.util.Constant;

import cn.aishua.R;

public class PopupMaker
{

    public static final int TYPE_VENDOR_CATEGORY = 0;
    public static final int TYPE_CANTON = 1;

    //	public static PopupMaker self;
    public static ResourceDataParser parser;

    private Context mContext;
    private LinearLayout mMain;
    private LinearLayout mLayoutLeft;
    private LinearLayout mLayoutRight;

    TextView mTarget;

    private OnFinishListener mOnFinishListener;

    private String color_left_back_normal = "#b1a599";
    private String color_left_back_selected = "#64574b";
    private String color_right_back_selected = "#f9a037";

    public int pId;
    public int id;

    public String pName; // parent name
    public String name; // name

    private boolean clickOnlyOnce = true;

    public PopupMaker(Context context)
    {
        mContext = context;
        if( parser == null )
        {
            parser = new ResourceDataParser( Constant.RESOURCE );
        }
    }

    public PopupMaker(Context context, int c, int bc)
    {
        this( context );
        pId = c;
        id = bc;

        clickOnlyOnce = false;
    }

    //	public synchronized static PopupMaker getInstance(Context context){
    //		if(self == null){
    //			self = new PopupMaker(context);
    //		}
    //		return self;
    //	}

    ArrayList<TextView> templeftTV = new ArrayList<TextView>();

    /**
     * 
     * @param main
     * @param target
     * @param targetId
     *            TextView to store id of the item (GONE)
     * @param targetPid
     *            TextView to store parent id of the item (GONE)
     * @param type
     * @param onFinishListener
     */
    public void make( LinearLayout main, TextView target, int type, OnFinishListener onFinishListener )
    {
        mMain = main;
        mTarget = target;
        mLayoutLeft = (LinearLayout) main.findViewById( R.id.linear1 );
        mLayoutRight = (LinearLayout) main.findViewById( R.id.linear2 );

        mOnFinishListener = onFinishListener;

        LinkedHashMap<Item, Map<Integer, Item>> temp;
        if( type == TYPE_VENDOR_CATEGORY )
        {
            temp = parser.category;
        }
        else
        {
            temp = parser.canton;
        }

        templeftTV.clear();
        tempRightTV.clear();
        for( final Entry<Item, Map<Integer, Item>> e : temp.entrySet() )
        {

            final TextView tv = (TextView) LayoutInflater.from( mContext ).inflate( R.layout.popup_item, null );
            tv.setLayoutParams( new LinearLayout.LayoutParams( LayoutParams.MATCH_PARENT, 67 ) );
            tv.setText( e.getKey().name );
            tv.setBackgroundColor( Color.parseColor( color_left_back_normal ) );
            tv.setTextColor( Color.WHITE );
            tv.setTag( e.getValue() );

            tv.setOnClickListener( new OnClickListener()
            {
                public void onClick( View v )
                {
                    for( int i = 0; i < templeftTV.size(); i++ )
                    {
                        templeftTV.get( i ).setBackgroundColor( Color.parseColor( color_left_back_normal ) );
                    }
                    tv.setBackgroundColor( Color.parseColor( color_left_back_selected ) );
                    buildRight( e.getValue() );
                    pId = e.getKey().id;
                    pName = e.getKey().name;
                }
            } );

            if( pId == e.getKey().id )
            {
                pName = e.getKey().name;
                tv.performClick();
            }

            templeftTV.add( tv );
            mLayoutLeft.addView( tv );
        }

    }

    ArrayList<TextView> tempRightTV = new ArrayList<TextView>();

    protected void buildRight( Map<Integer, Item> treeMap )
    {
        mLayoutRight.removeAllViews();
        for( final Entry<Integer, Item> e : treeMap.entrySet() )
        {
            final TextView tv = (TextView) LayoutInflater.from( mContext ).inflate( R.layout.popup_item, null );
            tv.setLayoutParams( new LinearLayout.LayoutParams( LayoutParams.MATCH_PARENT, 67 ) );
            tv.setText( e.getValue().name );
            tv.setBackgroundColor( Color.WHITE );
            tv.setTextColor( Color.BLACK );

            tv.setOnClickListener( new OnClickListener()
            {
                public void onClick( View v )
                {
                    id = e.getValue().id;
                    name = e.getValue().name;
                    if( mOnFinishListener != null )
                    {
                        mOnFinishListener.finish();
                    }

                    for( int i = 0; i < tempRightTV.size(); i++ )
                    {
                        tempRightTV.get( i ).setBackgroundColor( Color.WHITE );
                        tempRightTV.get( i ).setTextColor( Color.BLACK );
                    }

                    tv.setBackgroundColor( Color.parseColor( color_right_back_selected ) );
                    tv.setTextColor( Color.WHITE );

                    mTarget.setText( e.getValue().name );
                    mMain.setVisibility( View.GONE );
                }
            } );

            if( id == e.getValue().id )
            {
                tv.setBackgroundColor( Color.parseColor( color_right_back_selected ) );
                tv.setTextColor( Color.WHITE );
                name = e.getValue().name;
            }

            if( id == e.getValue().id && !clickOnlyOnce )
            {
                tv.performClick();
                clickOnlyOnce = true;
            }

            tempRightTV.add( tv );
            mLayoutRight.addView( tv );
        }
    }

    public interface OnFinishListener
    {
        void finish();
    }

}
